/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pojo.Giohang;
import util.HibernateUtil;

/**
 *
 * @author Katori
 */
public class GioHangDAO {

    public static List<Giohang> layTatCaGioHang() {
        List<Giohang> dsGiohang = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            String hql = "from pojo.Giohang";
            dsGiohang = ss.createQuery(hql).list();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return dsGiohang;
    }

    public static Giohang layGioHang(int maGioHang) {
        Giohang gio = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            gio = (Giohang) ss.get(Giohang.class, maGioHang);
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return gio;
    }

    public static boolean themGioHang(Giohang gio) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(gio);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e);
            return false;
        } finally {
            session.close();
        }
    }

    public static boolean capNhatGioHang(Giohang gio) {
        if (GioHangDAO.layGioHang(gio.getMaGioHang()) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            session.update(gio);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }

    public static boolean xoaGiohang(int maGiohang) {
        if (GioHangDAO.layGioHang(maGiohang) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            String hql = "update pojo.Giohang set BiXoa = :xoa where MaGiohang = :ma";
            Query query = session.createQuery(hql);
            query.setString("xoa", "1");
            query.setString("ma", Integer.toString(maGiohang));
            int rowCount = query.executeUpdate();
            session.getTransaction().commit();

            if (rowCount > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }
}
